package com.nsoft.server.service.daoImpl;

import java.util.List;

import javax.persistence.Query;

import com.nsoft.server.master.beans.VehicleMaster;
import com.nsoft.server.service.dao.VechileDao;
import com.nsoft.thickclient.trackMove.StringHelper;
import com.nsoft.thickclient.trackMove.intr.vehicle.ThinVehicle;
import com.nsoft.thickclient.trackMove.intr.vehicle.Vehicle;

public class VechileDaoImpl extends
		AbstractMasterGenericJPADAO<VehicleMaster, Integer> implements
		VechileDao {

	public List<ThinVehicle> getAllThinVechile(ThinVehicle thinVehicle) {
		String name = StringHelper.replace("*", "%", thinVehicle
				.getRegistrationNo());
		StringBuffer sb = new StringBuffer();
		sb
				.append("select New com.nsoft.thickclient.trackMove.intr.vehicle.ThinVehicle"
						+ "(master.vehNumId,master.vehCharRegno) from com.nsoft.server.master.beans.VehicleMaster"
						+ " master where 1=1 ");
		if (thinVehicle.getRegistrationNo() != null
				&& !thinVehicle.getRegistrationNo().equalsIgnoreCase("")
				&& !thinVehicle.getRegistrationNo().equalsIgnoreCase(" ")) {
			sb.append("and  master.vehCharRegno like '" + name + "'");
		}
		Query query = getEntityManager().createQuery(sb.toString());
		return query.getResultList();
	}

	public List<VehicleMaster> searchVechiles(Vehicle vehicle) {

		String name = "";
		StringBuffer sb = new StringBuffer();
		sb.append("select master "
				+ " from com.nsoft.server.master.beans.VehicleMaster"
				+ " master where 1=1 ");
		if (vehicle.getRegistrationNo() != null && !vehicle.getRegistrationNo().equalsIgnoreCase("")
				&& !vehicle.getRegistrationNo().equalsIgnoreCase(" ")) {
			name = StringHelper.replace("*", "%", vehicle.getRegistrationNo());
			sb.append("and  master.vehCharRegno like '" + name + "'");
		}

		if (vehicle.getLastDriverLicence() != null && !vehicle.getLastDriverLicence().equalsIgnoreCase("")
				&& !vehicle.getLastDriverLicence().equalsIgnoreCase(" ")) {
			name = StringHelper.replace("*", "%", vehicle.getLastDriverLicence());
			sb.append("and  master.vehCharDriverlicense like '" + name + "'");
		}

		if (vehicle.getLastDriverName() != null && !vehicle.getLastDriverName().equalsIgnoreCase("")
				&& !vehicle.getLastDriverName().equalsIgnoreCase(" ")) {
			name = StringHelper.replace("*", "%", vehicle.getLastDriverName());
			sb.append("and  master.vehCharLastdriver like '" + name + "'");
		}

		if (vehicle.getInsurence() != null && !vehicle.getInsurence().equalsIgnoreCase("")
				&& !vehicle.getInsurence().equalsIgnoreCase(" ")) {
			name = StringHelper.replace("*", "%", vehicle.getInsurence());
			sb.append("and  master.vehCharInsurance like '" + name + "'");
		}

		if (vehicle.getOwner() != null && !vehicle.getOwner().equalsIgnoreCase("")
				&& !vehicle.getOwner().equalsIgnoreCase(" ")) {
			name = StringHelper.replace("*", "%", vehicle.getOwner());
			sb.append("and  master.vehCharOwner like '" + name + "'");
		}

		Query query = getEntityManager().createQuery(sb.toString());
		return query.getResultList();
	}

}
